package ControlLayer;

import ModelLayer.*;

import java.util.ArrayList;
//model layer package has to be imported

/**
 * @author (Gruppe C) 
 * @version (2012.11.09)
 */
public class AddressCtr
{
    /*
     * Singleton - Only one instance allowed.
     */
    private static AddressCtr instance;
    private AddressCtr()
    {
        addressBook = addressBook.getInstance();
    }
    public static AddressCtr getInstance()
    {
        if (instance == null)
        {
            instance = new AddressCtr();
        }
        return instance;
    }
    
    
    // Fields
    private AddressBook addressBook;
    
    /*
     * Returns a person by id.
     * 
     * @param   id  ID of person to search for.
     * @return      Returns a person object with specified id.
     */
    public Person getPerson(long id)
    {
       Person person = addressBook.getPerson(id);
       return person;
    }
    
    /*
     * Returns a person by name.
     */
    public Person findPerson(String name)
    {
        return addressBook.findPerson(name);
    }
    
    /*
     * Returns a person id by name.
     */
    public long getPersonId(String name)
    {
        return addressBook.getPersonId(name);
    }
    
    //Vi opretter ny person, som bliver add'et til arraylisten<Person>.
    public void createPerson(String name, String address, String postalCode, String city, String phone)
    {   
      Person p = new Person(name, address, postalCode, city, phone); 
      addressBook.addPerson(p);
    }
    
    /*
     * Deletes a person by id.
     */
    public void deletePerson(long id)
    {
        addressBook.removePerson(id);
    }
    
    /*
     * Updates a person by id.
     * 
     * @param   id          ID of person to be modfied.
     * @param   newName     Modified person's new name.
     * @param   newAddress  Modified person's new address.
     */
    public void updatePerson(long id, String newName, String newAddress, String newPostalCode, String newCity, String newPhone)
    {
        Person p = addressBook.getPerson(id);
        p.setName(newName);
        p.setAddress(newAddress);
        p.setPostalCode(newPostalCode);
        p.setCity(newCity);
        p.setPhone(newPhone);
        
    }
   
    /*
     * Return all persons.
     */
    public ArrayList<Person> listAllPersons()
    {
          return addressBook.getAllPersons();
    }
}
